package com.squirrels.server.work;

import com.squirrels.server.annotation.NettyServerWorker;
import com.squirrels.server.common.constant.SystemConstant;
import com.squirrels.server.common.constant.UserConstant;
import com.squirrels.server.message.UserData;
import com.squirrels.server.message.protobuf.Message;
import com.squirrels.server.service.UserDataService;
import com.squirrels.server.utils.JWTUtils;
import dev.morphia.Datastore;
import lombok.extern.slf4j.Slf4j;

/**
 * 创建者: FlyingSquirrels
 * 创建时间: 2024/8/16 14:16
 * 描述: 大厅服务
 */

@Slf4j
@NettyServerWorker("hall")
public class HallWorker  extends ServerWorker{

    private static UserDataService dataService;

    /**
     * 为当前类注入service
     * @param datastore
     */
    private void doService(Datastore datastore) {
        dataService = UserDataService.operation(datastore);
    }
    @Override
    public Message.Entity doWorker(String operationBase) {
        // 未登录
        if (!JWTUtils.jwtUtils.checkToken(entity.getToken())) {
            return errorMsg(UserConstant.USER_NOT_LOGIN,entity);
        }
        // 注入 Service
        doService(datastore);
        Message.Entity responseData;
        switch (operationBase){
            case "userData" ->{
                responseData = userData();
            }
            case "create" -> {
                responseData = createData();
            }
            case "exists" -> {
                responseData = existsData();
            }
            default -> {
                responseData = errorMsg(UserConstant.OPERATION_NOT_ERROR,entity);
            }
        }
        return responseData;
    }

    // 检查用户是否创建
    private Message.Entity existsData() {
        String userId = JWTUtils.jwtUtils.getDataByJwtToken(entity.getToken());
        UserData userData = dataService.getUserDataById(userId);
        if (null == userData){
            return msg(UserConstant.USER_DATA_NOT_EXISTS,entity);
        }
        return msg(SystemConstant.OK,entity);
    }

    // 创建用户数据
    private Message.Entity createData() {
        Message.Hall hall = entity.getHall();
        String userId = JWTUtils.jwtUtils.getDataByJwtToken(entity.getToken());
        boolean userData = dataService.createUserData(userId, hall.getName(), hall.getImg(), hall.getIntro());
        if (userData == false){
            return errorMsg(SystemConstant.SERVER_ERROR,entity);
        }
        return msg(SystemConstant.OK,entity);
    }

    // 获取用户数据
    private Message.Entity userData() {
        String userId = JWTUtils.jwtUtils.getDataByJwtToken(entity.getToken());
        UserData userData = dataService.getUserDataById(userId);
        // 用户未创建
        if (userData == null){
            return errorMsg(UserConstant.USER_FIRST_LOGIN,entity);
        }
        // 用户已创建
        var data = Message.Hall.newBuilder()
                .setUid(String.valueOf(userData.getUid()))
                .setName(userData.getNickName())
                .setImg(userData.getHeadImg())
                .setIntro(userData.getSignature())
                .setPrice(String.valueOf(userData.getGold()))
                .build();
        return buildHallEntity(data, entity);
    }

    // 构建实体类
    private Message.Entity buildHallEntity(Message.Hall hall, Message.Entity entity){
        return  Message.Entity.newBuilder()
                    .setDataType(Message.Entity.EntityType.HallType)
                    .setOperation(entity.getOperation())
                    .setOperationBase(entity.getOperationBase())
                    .setHall(hall)
                    .setCode(200)
                .build();
    }
}
